package myview;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
/**
 * 
 * @author caizhiming
 * @created on 2015-4-13
 */
//package com.lff.test;
//
//import java.util.ArrayList;
//import java.util.List;
//
//import android.content.Context;
//import android.util.AttributeSet;
//import android.view.View;
//import android.view.ViewGroup;

public class XCFlowLayout extends ViewGroup 
{
	

	public XCFlowLayout(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
	}

	public XCFlowLayout(Context context, AttributeSet attrs)
	{
		this(context, attrs, 0);
	}

	public XCFlowLayout(Context context)
	{
		this(context, null);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
	{
		
		int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
		int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
		int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
		int modeHeight = MeasureSpec.getMode(heightMeasureSpec);

		// 濡傛灉鏄痺arp_content鎯呭喌涓嬶紝璁板綍瀹藉拰楂� 
		int width = 0;
		int height = 0;

		// 璁板綍姣忎竴琛岀殑瀹藉害涓庨珮搴�
		int lineWidth = 0;
		int lineHeight = 0;

		// 寰楀埌鍐呴儴鍏冪礌鐨勪釜鏁�
		int cCount = getChildCount();

		for (int i = 0; i < cCount; i++)
		{
			// 閫氳繃绱㈠紩鎷垮埌姣忎竴涓瓙view
			View child = getChildAt(i);
			// 娴嬮噺瀛怴iew鐨勫鍜岄珮,绯荤粺鎻愪緵鐨刴easureChild
			measureChild(child, widthMeasureSpec, heightMeasureSpec);
			// 寰楀埌LayoutParams
			MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

			// 瀛怴iew鍗犳嵁鐨勫搴�
			int childWidth = child.getMeasuredWidth() + lp.leftMargin
								+ lp.rightMargin;
			// 瀛怴iew鍗犳嵁鐨勯珮搴�
			int childHeight = child.getMeasuredHeight() + lp.topMargin
								+ lp.bottomMargin;

			// 鎹㈣ 鍒ゆ柇 褰撳墠鐨勫搴﹀ぇ浜�寮�緹鏂拌
			if (lineWidth + childWidth > sizeWidth - getPaddingLeft() - getPaddingRight())
			{
				// 瀵规瘮寰楀埌鏈�ぇ鐨勫搴�
				width = Math.max(width, lineWidth);
				// 閲嶇疆lineWidth
				lineWidth = childWidth;
				// 璁板綍琛岄珮
				height += lineHeight;
				lineHeight = childHeight;
			}
			else
			// 鏈崲琛�
			{
				// 鍙犲姞琛屽
				lineWidth += childWidth;
				// 寰楀埌褰撳墠琛屾渶澶х殑楂樺害
				lineHeight = Math.max(lineHeight, childHeight);
			}
			// 鐗规畩鎯呭喌,鏈�悗涓�釜鎺т欢
			if (i == cCount - 1)
			{
				width = Math.max(lineWidth, width);
				height += lineHeight;
			}
		}
		setMeasuredDimension(
								modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width + getPaddingLeft() + getPaddingRight(),
								modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height + getPaddingTop() + getPaddingBottom()//
		);

	}

	/**
	 * 瀛樺偍鎵�湁鐨刅iew
	 */
	private List<List<View>>	mAllViews	= new ArrayList<List<View>>();
	/**
	 * 姣忎竴琛岀殑楂樺害
	 */
	private List<Integer>		mLineHeight	= new ArrayList<Integer>();

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b)
	{
		mAllViews.clear();
		mLineHeight.clear();

		// 褰撳墠ViewGroup鐨勫搴�
		int width = getWidth();

		int lineWidth = 0;
		int lineHeight = 0;

		// 瀛樻斁姣忎竴琛岀殑瀛恦iew
		List<View> lineViews = new ArrayList<View>();

		int cCount = getChildCount();

		for (int i = 0; i < cCount; i++)
		{
			View child = getChildAt(i);
			MarginLayoutParams lp = (MarginLayoutParams) child
																.getLayoutParams();

			int childWidth = child.getMeasuredWidth();
			int childHeight = child.getMeasuredHeight();

			// 濡傛灉闇�鎹㈣
			if (childWidth + lineWidth + lp.leftMargin + lp.rightMargin > width - getPaddingLeft() - getPaddingRight())
			{
				// 璁板綍LineHeight
				mLineHeight.add(lineHeight);
				// 璁板綍褰撳墠琛岀殑Views
				mAllViews.add(lineViews);

				// 閲嶇疆鎴戜滑鐨勮瀹藉拰琛岄珮
				lineWidth = 0;
				lineHeight = childHeight + lp.topMargin + lp.bottomMargin;
				// 閲嶇疆鎴戜滑鐨刅iew闆嗗悎
				lineViews = new ArrayList<View>();
			}
			lineWidth += childWidth + lp.leftMargin + lp.rightMargin;
			lineHeight = Math.max(lineHeight, childHeight + lp.topMargin
												+ lp.bottomMargin);
			lineViews.add(child);

		}// for end
			// 澶勭悊鏈�悗涓�
		mLineHeight.add(lineHeight);
		mAllViews.add(lineViews);

		// 璁剧疆瀛怴iew鐨勪綅缃�

		int left = getPaddingLeft();
		int top = getPaddingTop();

		// 琛屾暟
		int lineNum = mAllViews.size();

		for (int i = 0; i < lineNum; i++)
		{
			// 褰撳墠琛岀殑鎵�湁鐨刅iew
			lineViews = mAllViews.get(i);
			lineHeight = mLineHeight.get(i);

			for (int j = 0; j < lineViews.size(); j++)
			{
				View	child = lineViews.get(j);
				// 鍒ゆ柇child鐨勭姸鎬�
				if (child.getVisibility() == View.GONE)
				{
					continue;
				}

				MarginLayoutParams lp = (MarginLayoutParams) child
																	.getLayoutParams();

				int lc = left + lp.leftMargin;
				int tc = top + lp.topMargin;
				int rc = lc + child.getMeasuredWidth();
				int bc = tc + child.getMeasuredHeight();

				// 涓哄瓙View杩涜甯冨眬
				child.layout(lc, tc, rc, bc);

				left += child.getMeasuredWidth() + lp.leftMargin
						+ lp.rightMargin;
			}
			left = getPaddingLeft();
			top += lineHeight;
		}

	}

	/**
	 * 涓庡綋鍓峍iewGroup瀵瑰簲鐨凩ayoutParams
	 */
	@Override
	public LayoutParams generateLayoutParams(AttributeSet attrs)
	{
		return new MarginLayoutParams(getContext(), attrs);
	}


}

